<route lang="json5" type="page">
{
  layout: 'tabbar',
  style: {
    navigationBarTitleText: '梦瑶诗歌',
    navigationStyle: 'default',
  },
  needLogin: 'true',
}
</route>

<template>
  <wd-toast />
  <view class="container">
    <!-- 分类标签页 -->
    <wd-tabs
      v-model="activeType"
      animated
      swipeable
      class="category-tabs"
      @change="handleTabChange"
    >
      <wd-tab v-for="item in typeList" :key="item.id" :name="item.type" :title="item.name"></wd-tab>
    </wd-tabs>

    <!-- 分类列表区域 -->
    <u-scroll-view scroll-y class="list-container">
      <view
        v-for="(item, index) in categoryList"
        :key="index"
        class="list-item"
        :style="{ '--index': index }"
        @click="navigateToAlbum(item.id)"
      >
        <text class="type-name">{{ item.name }}</text>
      </view>
    </u-scroll-view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { http } from '@/utils/http'
import { useToast } from 'wot-design-uni'
import { useAuthHeaders } from '@/utils/token'
import type { CategoryView } from '@/api/children.typing'

// 应用状态管理
const toast = useToast()
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL

// 响应式数据
const typeList = [
  { id: 1, name: '童声悠扬', type: 'audio' },
  { id: 2, name: '动画世界', type: 'video' },
]
const activeType = ref<string>(typeList[0].type)
const categoryList = ref<CategoryView[]>([])

// 获取分类列表
const getCategoryList = async (type: string) => {
  try {
    const res = await http.get<CategoryView[]>(
      `${API_BASE_URL}/api/v1/category/list`,
      {
        mediaType: type,
      },
      useAuthHeaders(),
    )

    if (res.code === 200) {
      categoryList.value = res.data || []
    } else {
      toast.error(`加载失败: ${res.msg}`)
    }
  } catch (err) {
    toast.error('网络请求异常')
  }
}

// 标签切换处理
const handleTabChange = ({ name }: { name: string }) => {
  activeType.value = name
  getCategoryList(name)
}

// 跳转到专辑页面
const navigateToAlbum = (categoryId: number) => {
  uni.navigateTo({
    url: `/pages/poem/children/children-album?categoryId=${categoryId}&type=${activeType.value}`,
  })
}

// 页面初次加载
onLoad(() => {
  usePageAuth()
})

// 页面生命周期
onMounted(() => {
  // 初始化加载默认分类
  getCategoryList(activeType.value)
})
</script>

<style scoped>
.container {
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;
  padding-top: 0;
  display: flex;
  flex-direction: column;
}

.category-tabs {
  position: sticky;
  top: 0;
  z-index: 10;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 6rpx 24rpx rgba(0, 0, 0, 0.12);
  border-radius: 0 0 30rpx 30rpx;
  padding: 20rpx 0;
}

.list-container {
  flex: 1;
  padding: 20rpx;
}

.list-item {
  padding: 32rpx 40rpx;
  margin: 20rpx 0;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.9) 0%, rgba(246, 248, 250, 0.9) 100%);
  border-radius: 20rpx;
  display: flex;
  align-items: center;
  position: relative;
  transition: all 0.3s ease;
  box-shadow: 0 8rpx 24rpx rgba(143, 157, 184, 0.12);
}

.list-item::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 8rpx;
  height: 60%;
  background: #5b8eff;
  border-radius: 0 8rpx 8rpx 0;
  transition: all 0.3s ease;
}

.list-item:active {
  transform: scale(0.98);
  box-shadow: 0 4rpx 12rpx rgba(143, 157, 184, 0.2);
}

.list-item:active::before {
  width: 12rpx;
  background: #3a6dd0;
}

.type-name {
  font-size: 34rpx;
  color: #2c3e50;
  font-weight: 500;
  position: relative;
  padding-left: 40rpx;
}

.type-name::before {
  content: '•';
  position: absolute;
  left: 0;
  color: #5b8eff;
  font-size: 48rpx;
  line-height: 1;
  top: -4rpx;
}
/* 入场动画 */
@keyframes itemEntrance {
  from {
    opacity: 0;
    transform: translateY(20rpx);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.list-item {
  animation: itemEntrance 0.4s ease forwards;
  animation-delay: calc(var(--index) * 0.08s);
  opacity: 0;
}
</style>
